/*
* Copyright (C) 2008 Google Enterprise EMEA
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.gsa.aasr;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;

import org.apache.log4j.Logger;

public class AASRSpreadsheetService extends AASRService {
    private static org.apache.log4j.Logger log4jLogger = null;
    private SpreadsheetService serv = null;
    
    public AASRSpreadsheetService() {
        log4jLogger = Logger.getLogger(AASRSpreadsheetService.class);
    }
    
    public void init(){
        log4jLogger.debug("Instantiating SpreadsheetService " + super.getName());
        serv = new SpreadsheetService(super.getName());
        log4jLogger.debug("SpreadsheetService instantiated");        
    }

    public void logClick(String[] params) throws AASRLogException{
        String frontend = null;
        
        if (serv.equals(null)){
            log4jLogger.error("SpreadsheetService not initialized. Use init() method.");
            throw new AASRLogException("SpreadsheetService not initialized");
        }
        ListEntry l;
        l = new ListEntry();

        for (int i = 0; i < params.length; i++) {
            String values[] = params[i].split("=");
            if ((values.length == 1) && (values[0].length() > 0))
                l.getCustomElements().setValueLocal(values[0], "0");
            if (values.length == 2) {
                if (values[0].equals("fend")) {
                    frontend = values[1];
                } else {
                    l.getCustomElements().setValueLocal(values[0], values[1]);
                    log4jLogger.debug("Info to log: " + values[0] + " = " + values[1]);
                }
            }
        }
        
        try {
            l.getCustomElements().setValueLocal("date", 
                                                super.now(getDateformat()));
            serv.setUserCredentials(this.getUser(), this.getPasswd());
            log4jLogger.debug("Credentials set: " + this.getUser() + ", " + this.getPasswd());
            log4jLogger.debug("Inserting in URL:" + this.getUrl());
            serv.insert(this.getUrl(), l);
            log4jLogger.debug("Values inserted in the Spreadsheet. Everything went OK");
        } catch (AuthenticationException e) {
            log4jLogger.error(e);
            throw new AASRLogException();
        } catch (ServiceException e) {
            log4jLogger.error(e.getMessage());
            log4jLogger.error(e);
            throw new AASRLogException();
        } catch (IOException e) {
            log4jLogger.error(e);
            throw new AASRLogException();
        }
    }
}
